package Prova::Controller::Assertion;

use strict;
use warnings;
use base 'Catalyst::Controller';
use Data::Dumper;

=head1 NAME

Prova::Controller::Assertion - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 create

create action

=cut

sub create : Private {
    my ( $self, $c ) = @_;

    my $datablock = $c->stash->{datablock};
    my $data = $c->stash->{assertion};

    # delete
    my $assertions = $c->forward( 'find_by_id', [ ( $datablock->id ) ] );
    if ( ref $assertions eq 'ARRAY' ) {
        for my $assertion ( @$assertions ) {
            $c->log->info( "delete assertion : id=".$assertion->id );
            $assertion->delete;
        }
    }

    # create
    if ( ref $data eq 'HASH' ) {
        my @new;
        for ( sort { $a <=> $b } keys %$data ) {
            $data->{$_}->{data_block_id} = $datablock->id;
            my $assertion = $c->model( 'DBIC::Assertion' )->create( $data->{$_} );
            push @new, $assertion;
            $c->log->info( "create assertion : id=".$assertion->id );
        }
         $c->stash->{assertion} = \@new;
    }
}

=head2 find_by_id

get assertion models having datablock_id

=cut

sub find_by_id : Private {
    my ( $self, $c, $datablock_id ) = @_;

    my $assertion = [ $c->model( 'DBIC::Assertion' )->search(
        { data_block_id => $datablock_id }
    ) ];

    return $assertion;
}

=head1 AUTHOR

Shanon Inc, E<lt>http://www.shanon.co.jp/E<gt>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
